package models;

import dto.Language;
import models.crudsiena.SienaSupport;
import play.i18n.Lang;
import services.MenuService;
import siena.Generator;
import siena.Id;
import siena.NotNull;

/**
 * Created by Gipxy group
 * Date: 4/2/12
 */
public class Menu extends SienaSupport {
    @Id(Generator.AUTO_INCREMENT)
    public Long id;

    @NotNull
    public String language;

    public String belongTo;   //product or app

    public String topicCode; //link to what topic

    @NotNull
    public int level; //1 or 2, 1=h2, 2=none

    public String menuName; // not use now

    public int index; //So thu tu xuat hien tren menu.

    @NotNull
    public int column;//having 2 columns


    public Menu() {
        this.language = Lang.get();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getTopicCode() {
        return topicCode;
    }

    public void setTopicCode(String topicCode) {
        this.topicCode = topicCode;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public String getMenuName() {
        return menuName;
    }

    public void setMenuName(String menuName) {
        this.menuName = menuName;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public String getBelongTo() {
        return belongTo;
    }

    public void setBelongTo(String belongTo) {
        this.belongTo = belongTo;
    }
    
    public String getTopicName() {
        Topic one = Topic.all().filter("code", topicCode).filter(Language.LANGUAGE_KEY, Lang.get()).get();
        
        return one == null ? "" : one.name;
    }

    @Override
    public void save() {
        super.save();
        MenuService.updateCache();
    }

    @Override
    public void insert() {
        super.insert();
        MenuService.updateCache();
    }

    @Override
    public void update() {
        super.update();
        MenuService.updateCache();
    }

    @Override
    public void delete() {
        super.delete();
        MenuService.updateCache();
    }
}
